package com.property.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.mapper.StaffMapper;
import com.property.pojo.Staff;
import com.property.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;

    //查询所有的员工
    @Override
    public String findAllStaff(Integer page) {
        PageHelper.startPage(page,10);
        List<Staff> list = staffMapper.findAllStaff();
        PageInfo<Staff> pageInfo = new PageInfo(list);
        return JSON.toJSONString(pageInfo);
    }

    //删除员工
    @Override
    public int deleteStaff(Integer sId) {
        return staffMapper.deleteStaff(sId);
    }

    //根据id查询员工
    @Override
    public Staff findBysId(Integer sId) {
        return staffMapper.findBysId(sId);
    }


    //新增更新员工
    public String updateOrSaveStaff(Staff staff){
        String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String message="操作成功";
        int num = 0;
        //判断数据是否为空 或者年龄工资是否小于0
        if(staff.getsName().equals("") || staff.getsName() ==null || staff.getsAge().equals("") || staff.getsAge() ==null || staff.getsAge()<=0||
                staff.getsMoney()<=0 || staff.getsMoney()==null || staff.getsPhone()==null ||staff.getsPhone().equals("") || staff.getsPost()==null || staff.getsPost().equals("")){
            message = "输入格式有误";
            //判断手机号是否正确
        }else if(!Pattern.matches(REGEX_MOBILE,staff.getsPhone())){
            message="手机号格式不正确";
            //如果sid为空 为新增
        }else{
            //判断sid是否存在  如果存在为修改 不存在为更新
            if(staff.getsId() == null || staff.getsId() == 0){
                staff.setsDate(new Date(System.currentTimeMillis()));
                num= staffMapper.saveStaff(staff);
            }else{
                num = staffMapper.updateStaff(staff);
            }
            if(num ==0){
                message = "操作失败";
            }
        }
        return JSON.toJSONString(message);
    }


}
